package com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.view.pulltorefresh.PullToRefreshBase;
import com.diudiutechnology.www.yadiudiu.util.view.pulltorefresh.PullToRefreshListView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.CleanBusiness;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyAdapter.CreditTutorialActivityAdapter;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.diudiutechnology.www.yadiudiu.R.id.listview;

/**
 * Created by Administrator on 2017/8/22.
 * 信贷列表
 */

public class CreditTutorialActivity extends Activity implements View.OnClickListener {
    public static final String GO_DETAIL = "go_detail";
    @Bind(R.id.retur)
    LinearLayout retur;
    Context context;
    @Bind(listview)
    PullToRefreshListView mPullToRefreshListView;
    @Bind(R.id.title_center)
    TextView titleCenter;
    CreditTutorialActivityAdapter adapter;
    private String mPos = "0";
    private List<CreditTutorialBean> mBean;
    private ListView mListView;
    private MyProgressDialog mDialog;
    // 请求数据
    private int mPageIndex = 1;
    private boolean mIsStart = true;//判断是否上下加载 true:下拉刷新  false:上拉加载数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cleaning);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        titleCenter.setText("信贷教程");
        mDialog = new MyProgressDialog(this);
        retur.setOnClickListener(this);
        mListView = mPullToRefreshListView.getRefreshableView();
        // 显示头部出现分割线
        mListView.setHeaderDividersEnabled(false);
        // 禁止底部出现分割线
        mListView.setFooterDividersEnabled(false);
        mListView.setDivider(new ColorDrawable(Color.TRANSPARENT));
        mListView.setDividerHeight(0);
        mPullToRefreshListView.setOnRefreshListener(refreshListener);
        initData();
    }

    private void getData() {
        if (mBean != null && mBean.size() > 0) {
            if (adapter == null) {
                adapter = new CreditTutorialActivityAdapter(CreditTutorialActivity.this, mBean);
                mListView.setAdapter(adapter);
            } else {
                adapter.addItem(mBean);
            }
        }
    }

    private void initData() {
        CleanBusiness.getCredit(String.valueOf(mPageIndex), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                mDialog.dismiss();
                mBean = CleanBusiness.parseToCreditBean(result);
                if (mBean != null && mBean.size() > 0) {
                    getData();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
        }
    }

    /**
     * 上拉刷新，下拉获取更多
     */
    PullToRefreshBase.OnRefreshListener refreshListener = new PullToRefreshBase.OnRefreshListener() {

        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            mIsStart = true;
            mPageIndex = 1;
            initData();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            mIsStart = false;
            mPageIndex++;
            getData();
        }
    };
}


